package com.foreveross.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.MessageService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.Message;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
/**
 * 消息通知
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/MessageController")
public class SysMessageController   extends BaseCostroller{

	private Logger log = Logger.getLogger(SysMessageController.class);

	@Resource
	private MessageService messageService;

	@Resource
	private LogService logService;
	
	@RequestMapping(method = RequestMethod.POST, value = "/getMessageList")
	public void getMessageList(HttpServletRequest request,
			HttpServletResponse response, String weixinId,String houseId,String title,
			String page,String pageSize,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)messageService.getMessageListCount(weixinId,houseId,title,userId);
			PageVo<Message> pageVo = new PageVo<Message>(page,total,pageSize);
			List<Message> list = messageService.getMessageList(weixinId,houseId,title,pageVo.getpageIndex(),pageVo.getpageSize(),userId);
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("消息通知 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("消息通知  查看失败");
			log.error("消息通知 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getMessageById")
	public void getMessageById(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			Message message = messageService.getMessageById(id);
			resultVo.setSuccess(true);
			resultVo.setData(message);
			log.info("消息通知 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("消息通知  查看失败");
			log.error("消息通知 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/updateMessage")
	public void updateMessage(HttpServletRequest request,
			HttpServletResponse response, Message message,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try{
			messageService.saveOrUpdate(message, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,updateType.equals(Constants.UPDATE_ADD)?Constants.LOG_ADD:Constants.LOG_UPDATE, "消息通知"+Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request), 
					Constants.getUpdateType(updateType)+"消息通知[" + message.toString()
							+ "]");
			log.info("消息通知 "+updateType+"成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("消息通知 "+updateType+"失败");
			log.error("消息通知"+updateType+"失败!",e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/deleteMessage")
	public void deleteMessage(HttpServletRequest request,
			HttpServletResponse response, 
			String ids) {
		ResultVo resultVo = new ResultVo();
		try{
			messageService.deleteMessage(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "通知消息删除",
					AuthUtil.getUserid(request), 
					"消息通知[" + ids+ "]");
			log.info("消息通知 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("消息通知 删除失败");
			log.info("消息通知 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}


}
